/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BotRidder;

/**
 *
 * @author user1
 */
import javax.microedition.lcdui.*;

public class Map1 extends Canvas implements CommandListener {
    private BotRidder midlet;
    private Command salir, ejecutar;
    private int x, y, orx, ory, i1, f1, i2, f2;
    
      
    public Map1(BotRidder midd){
        ejecutar = new Command("RUN", Command.SCREEN,1);
        salir = new Command("SALIR", Command.EXIT,1);
        this.midlet = midd;
        this.addCommand(ejecutar);
        this.addCommand(salir);
        this.setCommandListener(this);
                          
    }
    public void paint(Graphics g) {
        x = midlet.botti.getX();
        y = midlet.botti.getY();
        orx = getWidth();
        ory = getHeight();
        orx = (orx/2)-120;
        ory = (ory/2)-120;
        
        i1 = midlet.botti.ang1Inicio();
        f1 = midlet.botti.ang1Final();
        i2 = midlet.botti.ang2Inicio();
        f2 = midlet.botti.ang2Final();
        //Mapa
        g.translate(orx, ory);
        g.setColor(250,230,200);
        g.fillRect(0,0, 240,240);
        g.setColor(255,255,255);
        g.drawLine(30,0,30,240);
        g.drawLine(60,0,60,240);
        g.drawLine(90,0,90,240);
        g.drawLine(120,0,120,240);
        g.drawLine(150,0,150,240);
        g.drawLine(180,0,180,240);
        g.drawLine(210,0,210,240);
        g.drawLine(0,30,240,30);
        g.drawLine(0,60,240,60);
        g.drawLine(0,90,240,90);
        g.drawLine(0,120,240,120);
        g.drawLine(0,150,240,150);
        g.drawLine(0,180,240,180);
        g.drawLine(0,210,240,210);
        //Obstaculos        
        g.setColor(0,255,0);
        g.fillTriangle(105, 90, 90, 119, 119, 119);
        midlet.botti.ocupar(3, 3, 2);
        
        g.fillTriangle(75, 180, 60, 209, 89, 209);
        midlet.botti.ocupar(2, 6, 2);
        
        g.fillTriangle(165, 150, 150, 179, 179, 179);
        midlet.botti.ocupar(5, 5, 2);
        
        
        //Pintura
        g.setColor(250,250, 250);
        if (midlet.botti.getBrush()){
            midlet.botti.ocupar(x/30, y/30, 1);
        }else if(midlet.botti.getDesp()){
            midlet.botti.ocupar(x/30, y/30, 0);
        }
          
        for(int i=0; i<8;i++){
            for(int j=0; j<8; j++){
               if(midlet.botti.verMatriz(i, j) == 1 || midlet.botti.verMatriz(i, j) == 4) {
                   g.fillRect((i*30), (j*30), 30, 30);
               }
            }
       }
       //Objeto 
        g.setColor(0,0,255);
        for(int i=0; i<8;i++){
            for(int j=0; j<8; j++){
               if(midlet.botti.verMatriz(i, j) == 4 || midlet.botti.verMatriz(i, j) == 3) {
                   g.fillArc((i*30), (j*30), 30, 30, 0, 360);
               }
            }
       }
        
        //Bot
        g.setColor(100,100,100);
        g.drawRoundRect(x,y,30,30,5,5);
        g.fillRoundRect(x, y, 30, 30, 5, 5);
        g.setColor(150,150,150);
        g.fillRect(x+5,y+5,20,20);
        g.setColor(255,0,0);
        g.fillArc(x+i1, y+f1, 5, 5, 0, 360);
        g.fillArc(x+i2, y+f2, 5, 5, 0, 360);
        
    }
    public void commandAction(Command c, Displayable d){
        if (c == salir){
            midlet.salir();
        }
        if (c == ejecutar){
            this.programar();
        }
        
    }
    protected void keyPressed(int keyCode){
        switch(getGameAction(keyCode)){
            case Canvas.DOWN: {midlet.botti.south();break;}
            case Canvas.UP: {midlet.botti.north();break;}    
            case Canvas.LEFT: {midlet.botti.west();break;}
            case Canvas.RIGHT: {midlet.botti.east();break;}
            case Canvas.KEY_NUM2: {midlet.botti.move();break;}
            case Canvas.KEY_NUM8: {midlet.botti.back();break;}
            case Canvas.KEY_NUM6: {midlet.botti.right();break;}
            case Canvas.KEY_NUM4: {midlet.botti.left(); break;}
            case Canvas.FIRE: {midlet.botti.paint();break;}
            case Canvas.KEY_STAR: {midlet.botti.take();break;}
            case Canvas.KEY_POUND: {midlet.botti.drop();break;}    
                
        }
        this.repaint();        
        
        
        
    }
    
    public void programar(){
    midlet.botti.reiniciar();
    this.repaint();
        midlet.botti.right();
    this.repaint();
        midlet.botti.move();
    this.repaint();    
        midlet.botti.right();
    this.repaint();    
        midlet.botti.back();
    this.repaint();    
        midlet.botti.paint();
    this.repaint();    
        midlet.botti.south();
    this.repaint();    
        midlet.botti.south();
    this.repaint();    
        midlet.botti.nopaint();
    this.repaint();
    
}
    
    
    
    
}
